﻿using System;
using System.Web.UI;
using System.Web.Configuration;
using System.Data;
using System.Web.UI.WebControls;


public partial class manager_dept_Position : Page
{
    public string Html_Dept = "";
    public string Html_Dept_Detail = "";
    DBHelper db = new DBHelper(WebConfigurationManager.ConnectionStrings["connString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetDept();
            loadData();
        }
    }

    private void GetDept()
    {
        int i = 0;
        Html_Dept = "";
        DataTable dt = db.getList("select * from dict_dept where [isUsing]=1 order by id");
        foreach (DataRow dr in dt.Rows)
        {
            if (i == 0)
            {
                Html_Dept += "<li onclick=\"SetDept('" + dr["id"].ToString() + "','" + dr["mc"].ToString() + "')\" role=\"presentation\" class=\"active\"><a href=\"#dept_" + dr["id"].ToString() + "\" role=\"tab\" data-toggle=\"tab\">" + dr["mc"].ToString() + "</a></li>";
            }
            else
            {
                Html_Dept += "<li onclick=\"SetDept('" + dr["id"].ToString() + "','" + dr["mc"].ToString() + "')\"  role=\"presentation\"><a href=\"#dept_" + dr["id"].ToString() + "\" role=\"tab\" data-toggle=\"tab\">" + dr["mc"].ToString() + "</a></li>";
            }
            i = 1;
        }
    }

    private void loadData()
    {
        dept_id.Value = "";
        int i = 0;
        DataTable dt = db.getList("select * from dict_dept where [isUsing]=1 order by id");
        foreach (DataRow dr in dt.Rows)
        {
            dept_id.Value = dept_id.Value + "|" + dr["id"].ToString();
            if (i == 0)
            { Html_Dept_Detail += "<div role=\"tabpanel\" class=\"tab-pane active\" id=\"dept_" + dr["id"].ToString() + "\">"; }
            else
            { Html_Dept_Detail += "<div role=\"tabpanel\" class=\"tab-pane\" id=\"dept_" + dr["id"].ToString() + "\">"; }

            Html_Dept_Detail += "<div class=\"box-body\">";
            Html_Dept_Detail += "<table id=\"table_" + dr["id"].ToString() + "\" class=\"table table-bordered table-striped\">";
            Html_Dept_Detail += "<thead>";
            Html_Dept_Detail += "<tr>";
            Html_Dept_Detail += "<th></th>";
            Html_Dept_Detail += "<th class=\"col-sm-5\">名称</th>";
            Html_Dept_Detail += "<th class=\"col-sm-2\">类型</th>";
            Html_Dept_Detail += "<th class=\"col-sm-1\">当前在岗</th>";
            Html_Dept_Detail += "<th class=\"col-sm-1\">最后考评</th>";
            Html_Dept_Detail += "<th class=\"col-sm-1\">自动</th>";
            Html_Dept_Detail += "<th class=\"col-sm-1\">状态</th>";
            Html_Dept_Detail += "<th class=\"col-sm-1\">操作</th>";
            Html_Dept_Detail += "</tr>";
            Html_Dept_Detail += "</thead>";
            Html_Dept_Detail += "<tbody>";
            DataTable dt1 = db.getList("v_dict_dept_position", "*", "dept=" + dr["id"].ToString(), "type,bh");
            foreach (DataRow dr1 in dt1.Rows)
            {
                Html_Dept_Detail += "<asp:Repeater runat=\"server\">";
                Html_Dept_Detail += "<ItemTemplate>";
                Html_Dept_Detail += "<tr>";
                Html_Dept_Detail += "<td>" + dr1["id"].ToString() + "</td>";
                switch (dr1["type"].ToString())
                {
                    case "1":
                        Html_Dept_Detail += "<td><b>" + dr1["mc"].ToString() + "</b></td>";
                        Html_Dept_Detail += "<td>" + SQ_Eval_Pub.GetPsType(dr1["type"].ToString()) + "</td>";
                        break;
                    case "2":
                        Html_Dept_Detail += "<td style=\"padding-left:30px;\">" + dr1["mc"].ToString() + "</td>";
                        Html_Dept_Detail += "<td style=\"padding-left:15px;\">" + SQ_Eval_Pub.GetPsType(dr1["type"].ToString()) + "</td>";
                        break;
                    case "3":
                        Html_Dept_Detail += "<td style=\"padding-left:50px;\">" + dr1["mc"].ToString() + "</td>";
                        Html_Dept_Detail += "<td style=\"padding-left:25px;\">" + SQ_Eval_Pub.GetPsType(dr1["type"].ToString()) + "</td>";
                        break;
                }



                Html_Dept_Detail += "<td><b>" + dr1["userName"].ToString() + "</b></td>";
                Html_Dept_Detail += "<td>" + dr1["LastYear"].ToString() + "年" + dr1["LastMonth"].ToString() + "月" + "</td>";
                Html_Dept_Detail += "<td>" + SQ_Eval_Pub.GetIsZd(dr1["iszdwc"].ToString()) + "</td>";
                Html_Dept_Detail += "<td>" + SQ_Eval_Pub.GetIsUsing(dr1["isUsing"].ToString()) + "</td>";
                Html_Dept_Detail += "<td><a data-toggle=\"modal\" data-target=\"#userManager\" data-action=\"update\" data-bid=\"" + dr1["id"].ToString() + "\">修改</a></td>";
                Html_Dept_Detail += "</tr>";
            }
            Html_Dept_Detail += "</tbody>";
            Html_Dept_Detail += "</table>";
            Html_Dept_Detail += "</div>";
            Html_Dept_Detail += "</div>";
            i = 1;
        }



        DataTable dtType = db.getList("[dict_dept_position_type]", "ID,mc", "isUsing=1", "id desc");
        dllType.DataSource = dtType;
        dllType.DataTextField = "mc";
        dllType.DataValueField = "ID";
        dllType.DataBind();





    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string errmsg = "";
        string mc = txtMc.Text.Trim();
        string dept = dept_id_now.Value.Trim();
        string parentid = dllParent.SelectedValue;
        string type = dllType.SelectedValue;
        string userbh = dllUser.SelectedValue;
        string isUsing = ddlIsUsing.SelectedValue;
        string iszdwc = ddlIsZdwc.SelectedValue;

        string sql = "insert into dict_dept_position (dept,parentid,type,mc,userbh,isUsing,iszdwc) values (" + dept + "," + parentid + "," + type + ",N'" + mc + "','" + userbh + "'," + isUsing + ",'" + iszdwc + "')";

        int i = db.executeNoneQuery(sql, ref errmsg);
        if (i > 0)
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("添加成功", Request.Url.LocalPath + Request.Url.Query));
        }
        else
        {
            Response.Write(CommonHelper.alertMsg("添加失败," + errmsg));
        }




    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string errmsg = "";
        string mc = txtMc.Text.Trim();
        string dept = dept_id_now.Value.Trim();
        string parentid = dllParent.SelectedValue;
        string type = dllType.SelectedValue;
        string userbh = dllUser.SelectedValue;
        string isUsing = ddlIsUsing.SelectedValue;
        string iszdwc = ddlIsZdwc.SelectedValue;

        string id = hidBID.Value;

        string sql = "update dict_dept_position set dept=" + dept + ",parentid=" + parentid + ",type=" + type + ",mc=N'" + mc + "',userbh='" + userbh + "',isUsing=" + isUsing + ",iszdwc='" + iszdwc + "' where id=" + id;
        int i = db.executeNoneQuery(sql, ref errmsg);
        if (i > 0)
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("修改成功", Request.Url.LocalPath + Request.Url.Query));
        }
        else
        {
            Response.Write(CommonHelper.alertMsg("修改失败," + errmsg));
        }

    }

    protected void btnGet_Click(object sender, EventArgs e)
    {
        DataTable dtParent = db.getList("[dict_dept_position]", "ID,mc", "isUsing=1 and type in(1,2) and dept=" + dept_id_now.Value, "mc");
        dllParent.DataSource = dtParent;
        dllParent.DataTextField = "mc";
        dllParent.DataValueField = "ID";
        dllParent.DataBind();
        dllParent.Items.Insert(0, new ListItem("-无-", "0"));

        DataTable dtUser;

        if (hidBID.Value != "")
        {
            string sesql = "select userName,userbh from [v_user] where isUsing=1 and (psid='' or psid is null) and dept=" + dept_id_now.Value;
            sesql += " union";
            sesql += "  select userName,userbh from[v_user] where userbh = (select userbh from dict_dept_position where id = " + hidBID.Value + ")";

            dtUser = db.getList(sesql);
            dllUser.DataSource = dtUser;
            dllUser.DataTextField = "userName";
            dllUser.DataValueField = "userbh";
            dllUser.DataBind();
            dllUser.Items.Insert(0, new ListItem("-无-", "0"));

            DataTable Dt = db.getList("[dict_dept_position]", "*", "id=" + hidBID.Value, "id");
            if (Dt.Rows.Count == 1)
            {
                txtMc.Text = Dt.Rows[0]["mc"].ToString();
                dllParent.SelectedValue = Dt.Rows[0]["parentid"].ToString();
                dllType.SelectedValue = Dt.Rows[0]["type"].ToString();
                dllUser.SelectedValue = Dt.Rows[0]["userbh"].ToString();
                ddlIsUsing.SelectedValue = Dt.Rows[0]["isUsing"].ToString().Trim() == "True" ? "1" : "0";
                ddlIsZdwc.SelectedValue = Dt.Rows[0]["iszdwc"].ToString().Trim() == "True" ? "1" : "0";
            }
        }
        else
        {
            dtUser = db.getList("[v_user]", "userName,userbh", "isUsing=1 and (psid='' or psid is null) and dept=" + dept_id_now.Value, "id");
            dllUser.DataSource = dtUser;
            dllUser.DataTextField = "userName";
            dllUser.DataValueField = "userbh";
            dllUser.DataBind();
            dllUser.Items.Insert(0, new ListItem("-无-", "0"));
        }



    }

}